<?php


namespace app\index\controller;

use think\Request;
use app\index\model\Notice as NoticeModel;

class Notice extends Base
{
    //通知列表
    public function  noticeList()
    {
        $this -> view -> assign('title', '通知列表');
        $this -> view -> assign('keywords', '软件工程专业教学管理系统');
        $this -> view -> assign('desc', '软件工程专业教学管理系统');

        $this -> view -> count = NoticeModel::count();

        $list = NoticeModel::paginate(10);

        $this -> view -> assign('list', $list);
        //渲染通知列表模板
        return $this -> view -> fetch('notice_list');
    }

    //渲染编辑通知界面
    public function noticeEdit(Request $request)
    {
        $notice_id = $request -> param('id');
        $result = NoticeModel::get($notice_id);
        $this->view->assign('title','编辑通知');
        $this->view->assign('keywords','软件工程专业教学管理系统');
        $this->view->assign('desc','软件工程专业教学管理系统');
        $this->view->assign('notice_info',$result->getData());
        return $this->view->fetch('notice_edit');
    }

    //更新数据操作
    public function editNotice(Request $request)
    {
        $data = $request -> param();

        //设置更新条件
        $condition = ['id'=>$data['id']];

        //更新当前记录
        $result = NoticeModel::update($data,$condition);

        //设置返回数据
        $status = 0;
        $message = '更新失败,请检查';

        //检测更新结果,将结果返回给notice_edit模板中的ajax提交回调处理
        if (true == $result) {
            $status = 1;
            $message = '恭喜, 更新成功~~';
        }
        return ['status'=>$status, 'message'=>$message];
    }

    //删除操作
    public function deleteNotice(Request $request)
    {
        $user_id = $request -> param('id');
        NoticeModel::update(['is_delete'=>1],['id'=> $user_id]);
        NoticeModel::destroy($user_id);

    }

    //恢复删除操作
    public function unDelete()
    {
        NoticeModel::update(['delete_time'=>NULL],['is_delete'=>1]);
    }

    //添加操作的界面
    public function  noticeAdd()
    {
        $this->view->assign('title','添加通知');
        $this->view->assign('keywords','软件工程专业教学管理系统');
        $this->view->assign('desc','软件工程专业教学管理系统');
        return $this->view->fetch('notice_add');
    }

    //添加操作
    public function addNotice(Request $request)
    {
        $data = $request -> param();
        $status = 1;
        $message = '添加成功';

        $rule = [
            'title|标题' => "require|min:3|max:10",
            'description|描述' => "require|min:3|max:20",
            'content|内容' => 'require|min:3'
        ];

        $result = $this -> validate($data, $rule);

        if ($result === true) {
            $user= NoticeModel::create($request->param());
            if ($user === null) {
                $status = 0;
                $message = '添加失败~~';
            }
        }
        return ['status'=>$status, 'message'=>$message];
    }
}